home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 115_01.zip / ED9.C < prev    next >
Text File  |  1993-06-01  |  5KB  |  294 lines

  1. /* Screen editor:  general utilities
  2.  *
  3.  * Source:  ed9.c
  4.  * Version: May 3, 1981.
  5.  */
  6.  
  7. /* convert lower case to upper case */
  8.  
  9. toupper(c) int c;
  10. {
  11.     if ((c<'a')|(c>'z')) {
  12.         return(c);
  13.     }
  14.     else {
  15.         return(c-32);
  16.     }
  17. }
  18.  
  19. /* convert upper case to lower case */
  20.  
  21. tolower(c) int c;
  22. {
  23.     if ((c<'A')|(c>'Z')) {
  24.         return(c);
  25.     }
  26.     else {
  27.         return(c+32);
  28.     }
  29. }
  30.  
  31. /* return: is first token in args a number ? */
  32. /* return value of number in *val            */
  33.  
  34. number(args,val) char *args; int *val;
  35. {
  36. char c;
  37.     c=*args++;
  38.     if ((c<'0')|(c>'9')) {
  39.         return(NO);
  40.     }
  41.     *val=c-'0';
  42.     while (c=*args++) {
  43.         if ((c<'0')|(c>'9')) {
  44.             break;
  45.         }
  46.         *val=(*val*10)+c-'0';
  47.     }
  48.     return(YES);
  49. }
  50.  
  51. /* convert character buffer to numeric */
  52.  
  53. ctoi(buf,index) char *buf; int index;
  54. {
  55. int k;
  56.     while ( (buf[index]==' ') |
  57.         (buf[index]==TAB) ) {
  58.         index++;
  59.     }
  60.     k=0;
  61.     while ((buf[index]>='0')&(buf[index]<='9')) {
  62.         k=(k*10)+buf[index]-'0';
  63.         index++;
  64.     }
  65.     return(k);
  66. }
  67.  
  68.  
  69. /* return maximum of m,n */
  70.  
  71. max(m,n) int m,n;
  72. {
  73.     if (m>=n) {
  74.         return(m);
  75.     }
  76.     else {
  77.         return(n);
  78.     }
  79. }
  80.  
  81. /* return minimum of m,n */
  82.  
  83. min(m,n) int m,n;
  84. {
  85.     if (m<=n) {
  86.         return(m);
  87.     }
  88.     else {
  89.         return(n);
  90.     }
  91. }
  92.  
  93. /* put decimal integer n in field width >= w.
  94.  * left justify the number in the field.
  95.  */
  96.  
  97. putdec(n,w) int n,w;
  98. {
  99. char chars[10];
  100. int i,nd;
  101.     nd=itoc(n,chars,10);
  102.     i=0;
  103.     while (i<nd) {
  104.         syscout(chars[i++]);
  105.     }
  106.     i=nd;
  107.     while (i++<w) {
  108.         syscout(' ');
  109.     }
  110. }
  111.  
  112. /* convert integer n to character string in str */
  113.  
  114. itoc(n,str,size) int n; char *str; int size;
  115. {
  116. int absval;
  117. int len;
  118. int i,j,k;
  119.     absval=abs(n);
  120.     /* generate digits */
  121.     str[0]=0;
  122.     i=1;
  123.     while (i<size) {
  124.         str[i++]=(absval%10)+'0';
  125.         absval=absval/10;
  126.         if (absval==0) {
  127.             break;
  128.         }
  129.     }
  130.     /* generate sign */
  131.     if ((i<size)&(n<0)) {
  132.         str[i++]='-';
  133.     }
  134.     len=i-1;
  135.     /* reverse sign, digits */
  136.     i--;
  137.     j=0;
  138.     while (j<i) {
  139.         k=str[i];
  140.         str[i]=str[j];
  141.         str[j]=k;
  142.         i--;
  143.         j++;
  144.     }
  145.     return(len);
  146. }
  147.  
  148. /* return absolute value of n */
  149.  
  150. abs(n) int n;
  151. {
  152.     if (n<0) {
  153.         return(-n);
  154.     }
  155.     else {
  156.         return(n);
  157.     }
  158. }
  159.  
  160. /* system error routine */
  161.  
  162. syserr(s) char *s;
  163. {
  164.     pmtmess("system error: ",s);
  165. }
  166.  
  167. /* user error routine */
  168.  
  169. error(s) char *s;
  170. {
  171.     pmtmess("error: ",s);
  172. }
  173.  
  174. /* disk error routine */
  175.  
  176. diskerr(s) char *s;
  177. {
  178.     pmtmess("disk error: ",s);
  179. }
  180.  
  181. /* read the next line of the file into
  182.  * the buffer of size n that p points to.
  183.  * Successive calls to readline() read the file
  184.  * from front to back.
  185.  */
  186.  
  187. readline(file,p,n) int file; char *p; int n;
  188. {
  189. int c;
  190. int k;
  191.     k=0;
  192.     while (1) {
  193.         c=sysrdch(file);
  194.         if (c==ERR) {
  195.             return(ERR);
  196.         }
  197.         if (c==EOF) {
  198.             /* ignore line without CR */
  199.             return (EOF);
  200.         }
  201.         if (c==CR) {
  202.             return(k);
  203.         }
  204.         if (k<n) {
  205.             /* move char to buffer */
  206.             *p++=c;
  207.         }
  208.         /* always bump count */
  209.         k++;
  210.     }
  211. }
  212.  
  213. /* push (same as write) line to end of file.
  214.  * line is in the buffer of size n that p points to.
  215.  * lines written by this routine may be read by
  216.  * either readline() or popline().
  217.  */
  218.  
  219. pushline(file,p,n) int file; char *p; int n;
  220. {
  221.     /* write all but trailing CR */
  222.     while ((n--)>0) {
  223.         if (syspshch(*p++,file)==ERR) {
  224.             return(ERR);
  225.         }
  226.     }
  227.     /* write trailing CR */
  228.     return(syspshch(CR,file));
  229. }
  230.  
  231. /* pop a line from the back of the file.
  232.  * the line should have been pushed using pushline().
  233.  */
  234.  
  235. popline(file,p,n) int file; char *p; int n;
  236. {
  237. int c;
  238. int k, kmax, t;
  239.     /* first char must be CR */
  240.     c=syspopch(file);
  241.     if (c==EOF) {
  242.         /* at START of file */
  243.         return(EOF);
  244.     }
  245.     if (c==CR) {
  246.         /* put into buffer */
  247.         *p++=CR;
  248.         k=1;
  249.     }
  250.     else {
  251.         syserr("popline: missing CR");
  252.         return(ERR);
  253.     }
  254.     /* pop line into buffer in reverse order */
  255.     while (1) {
  256.         c=syspopch(file);
  257.         if (c==ERR) {
  258.             return(ERR);
  259.         }
  260.         if (c==EOF) {
  261.             break;
  262.         }
  263.         if (c==CR) {
  264.             /* this ends ANOTHER line */
  265.             /* push it back           */
  266.             if (syspshch(CR,file)==ERR) {
  267.                 return(ERR);
  268.             }
  269.             break;
  270.         }
  271.         /* non-special case */
  272.         if (k<n) {
  273.             /* put into buffer */
  274.             *p++=c;
  275.         }
  276.         /* always bump count */
  277.         k++;
  278.     }
  279.     /* remember if we truncated the line */
  280.     kmax=k;
  281.     /* reverse the buffer */
  282.     k=min(k,n-1);
  283.     t=0;
  284.     while (k>t) {
  285.         /* swap p[t], p[k] */
  286.         c=p[k];
  287.         p[k]=p[t];
  288.         p[t]=c;
  289.         k--;
  290.         t++;
  291.     }
  292.     return(kmax);
  293. }
  294.